package com.mjh.zhujianju.controller.base;

import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.base.PraiseEO;
import com.mjh.zhujianju.service.base.PraiseService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author liuyang
 * @Description: 点赞
 * @date 2018/8/12 18:10
 */
@RestController
@RequestMapping("/praise")
@Api("点赞接口")
public class PraiseController {

    @Autowired
    private PraiseService praiseService;

    @ApiOperation(value="点赞-添加", notes="点赞请求，添加点赞记录{result:ok ->成功，error->异常，parameter->参数异常，repeat->重复}")
    @RequestMapping(value = "/add/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "业务id", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<JsonResult> add (@PathVariable(value = "id")Long id){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                PraiseEO praiseEO = new PraiseEO();
                praiseEO.setUserId(userId);
                praiseEO.setCreateUserId(userId);
                praiseEO.setBusinessId(id);
                praiseEO.setType(BusinessEnum.TYPE_WORKDYNAMIC.getCode());
                String res = praiseService.savePraiseEO(praiseEO);
                result.setStatus(res);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="点赞-撤回", notes="撤回用户对某个点赞的记录{result:ok ->成功，error->异常，parameter->参数异常，repeat->重复}")
    @ApiImplicitParam(name="点赞实体类", value="PraiseEO", required = true, dataType = "PraiseEO")
    @RequestMapping(value = "/revoke", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> revoke (@RequestBody  PraiseEO praiseEO){
        JsonResult result = new JsonResult();
        try {
            Long userId = XTCurrentUser.getCurrentUserId();
            if (null !=userId){
                praiseEO.setUserId(userId);
                String res = praiseService.revokePraiseEO(praiseEO);
                result.setResult(res);
                result.setStatus(ApiConstant.MSG_SUCCESS);
            }else {
                result.setResult("请先登录");
                result.setStatus(ApiConstant.MSG_NO_USER);
                return ResponseEntity.ok(result);
            }
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

}
